package com.eascs.ticket.ticket.service;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.common.util.ObjectUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketChangeLog;
import com.eascs.web.sso.UserHolder;

@Service
public class TicketChangeLogService extends BaseSevice {

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketChangeLog(TicketChangeLog ticketChangeLog) {
		this.proxyDao.insert(ticketChangeLog);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketChangeLog(String ticketId, String type, String value) {
		TicketChangeLog ticketChangeLog = new TicketChangeLog();
		ticketChangeLog.setTicketId(ticketId);
		ticketChangeLog.setType(type);
		ticketChangeLog.setValue(value);
		ticketChangeLog.setCreator(UserHolder.getUser().getCode());
		ticketChangeLog.setCreateDate(new Date());
		this.insertTicketChangeLog(ticketChangeLog);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketChangeLogWithObject(String ticketId, String type, Object value) {
		insertTicketChangeLog(ticketId, type, ObjectUtil.toJson(value));
	}

	public List<Object[]> getTicketChangeLogsByTicketId(String ticketId) {
		return this.proxyDao.findObjectsList("select tcl.id,tcl.ticket_id,tcl.type,u.name,tcl.create_date from ticket_change_log tcl left join user u on tcl.creator=u.code where tcl.ticket_id=? order by tcl.create_date", ticketId);
	}
}
